package com.jic.veteran.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.component.VeteranSecurityContext;
import com.jic.veteran.dao.AlumniSchoolAdminMapper;
import com.jic.veteran.dto.AlumniAddAccommodationParam;
import com.jic.veteran.dto.AlumniAddNewsParam;
import com.jic.veteran.dto.AlumniAddTeacherParam;
import com.jic.veteran.dto.AlumniSchoolInfoParam;
import com.jic.veteran.mapper.AlumniSchoolAccommodationMapper;
import com.jic.veteran.mapper.AlumniSchoolNewsMapper;
import com.jic.veteran.mapper.AlumniSchoolTeachersMapper;
import com.jic.veteran.model.*;
import com.jic.veteran.service.AlumniSchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 学校信息Service实现类
 * Created by mars on 2021/11/24.
 */
@Slf4j
@Service
public class AlumniSchoolServiceImpl implements AlumniSchoolService {

    @Autowired
    private AlumniSchoolAdminMapper alumniSchoolAdminMapper;

    @Autowired
    private AlumniSchoolTeachersMapper alumniSchoolTeachersMapper;

    @Autowired
    private AlumniSchoolNewsMapper alumniSchoolNewsMapper;

    @Autowired
    private AlumniSchoolAccommodationMapper alumniSchoolAccommodationMapper;

    /**
     * by mars
     * @param alumniSchoolInfoParam 学校信息修改
     * @return
     */
    public int schoolInfo(AlumniSchoolInfoParam alumniSchoolInfoParam) {
//        System.out.println(VeteranSecurityContext.getUserName());
//        System.out.println(DateUtil.date());
        return alumniSchoolAdminMapper.schoolInfo(alumniSchoolInfoParam.getContent(),
                alumniSchoolInfoParam.getSchoolName(),
                alumniSchoolInfoParam.getIcon1(),
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }


    /**
     * 获取学校分页老师
     * Created by mars on 2021/11/26.
     */
    @Override
    public List<AlumniSchoolTeachers> getPageTeachers(Integer pageSize,Integer pageNum,String teacherName,Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        List<AlumniSchoolTeachers> alumniSchoolTeachers;

        AlumniSchoolTeachersExample alumniSchoolTeachersExample = new AlumniSchoolTeachersExample();
        AlumniSchoolTeachersExample.Criteria criteria = alumniSchoolTeachersExample.createCriteria();

        if(status != 3){
            criteria.andStatusEqualTo(status);
        }
        if(teacherName.length() > 0){
            criteria.andTeacherNameLike("%"+teacherName+"%");
        }
        alumniSchoolTeachersExample.setOrderByClause("create_time desc");
        alumniSchoolTeachers = alumniSchoolTeachersMapper.selectByExampleWithBLOBs(alumniSchoolTeachersExample);
        return alumniSchoolTeachers;
    }
    /**
     * by mars
     * @param id,status 更新展示状态
     * @return
     */
    @Override
    public int upTeacherStatus(Integer id, Integer status) {

        return alumniSchoolAdminMapper.upTeacherStatus(id,status);
    }

    /**
     * by mars
     * @param alumniAddTeacherParam 新增教师
     * @return
     */
    public int addTeacher(AlumniAddTeacherParam alumniAddTeacherParam) {
        return alumniSchoolAdminMapper.addTeacher(alumniAddTeacherParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    /**
     * by mars
     * @param id
     * @param alumniAddTeacherParam 新增教师
     * @return
     */
    @Override
    public int aditTeacher(Integer id, AlumniAddTeacherParam alumniAddTeacherParam) {

        return alumniSchoolAdminMapper.aditTeacher(id,
                alumniAddTeacherParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }


    /**
     * by mars
     * @param id 删除教师
     * @return
     */
    @Override
    public int deleteTeacher(Integer id) {

        return alumniSchoolTeachersMapper.deleteByPrimaryKey(id.longValue());
    }


    /**
     * news 处理
     * @return
     */
    @Override
    public List<AlumniSchoolNews> getPageNews(Integer pageSize, Integer pageNum, String newsName, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        List<AlumniSchoolNews> alumniSchoolNews;

        AlumniSchoolNewsExample alumniSchoolNewsExample = new AlumniSchoolNewsExample();
        AlumniSchoolNewsExample.Criteria criteria = alumniSchoolNewsExample.createCriteria();

        if(status != 3){
            criteria.andStatusEqualTo(status);
        }
        if(newsName.length() > 0){
            criteria.andNewsNameLike("%"+newsName+"%");
        }
        alumniSchoolNewsExample.setOrderByClause("create_time desc");
        alumniSchoolNews = alumniSchoolNewsMapper.selectByExampleWithBLOBs(alumniSchoolNewsExample);
        return alumniSchoolNews;


    }

    @Override
    public int upNewsStatus(Integer id, Integer status) {
        return alumniSchoolAdminMapper.upNewsStatus(id,status);
    }

    @Override
    public int addNews(AlumniAddNewsParam alumniAddNewsParam) {

        return alumniSchoolAdminMapper.addNews(alumniAddNewsParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    @Override
    public int aditNews(Integer id, AlumniAddNewsParam alumniAddNewsParam) {

        return alumniSchoolAdminMapper.aditNews(id,
                alumniAddNewsParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    @Override
    public int deleteNews(Integer id) {
        return alumniSchoolNewsMapper.deleteByPrimaryKey(id.longValue());

    }



    /**
     * 周边食宿 处理
     * @return
     */
    @Override
    public List<AlumniSchoolAccommodation> getPageAccommodation(Integer pageSize, Integer pageNum, String accommodationName, Integer type) {


        PageHelper.startPage(pageNum, pageSize);
        List<AlumniSchoolAccommodation> AlumniSchoolAccommodation;

        AlumniSchoolAccommodationExample alumniSchoolAccommodationExample = new AlumniSchoolAccommodationExample();
        AlumniSchoolAccommodationExample.Criteria criteria = alumniSchoolAccommodationExample.createCriteria();

        if(type != 3){
            criteria.andTypeEqualTo(type);
        }
        if(accommodationName.length() > 0){
            criteria.andAccommodationNameLike("%"+accommodationName+"%");
        }
        alumniSchoolAccommodationExample.setOrderByClause("create_time desc");
        AlumniSchoolAccommodation = alumniSchoolAccommodationMapper.selectByExampleWithBLOBs(alumniSchoolAccommodationExample);
        return AlumniSchoolAccommodation;

    }

    @Override
    public int addAccommodation(AlumniAddAccommodationParam alumniAddAccommodationParam) {

        System.out.println("add"+VeteranSecurityContext.getUser());

        System.out.println("add"+SecurityContextHolder.getContext().getAuthentication());

        return alumniSchoolAdminMapper.addAccommodation(alumniAddAccommodationParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    @Override
    public int aditAccommodation(Integer id, AlumniAddAccommodationParam alumniAddAccommodationParam) {
        return alumniSchoolAdminMapper.aditAccommodation(id,
                alumniAddAccommodationParam,
                VeteranSecurityContext.getUserName(),
                DateUtil.date());
    }

    @Override
    public int deleteAccommodation(Integer id) {
        return alumniSchoolAccommodationMapper.deleteByPrimaryKey(id.longValue());
    }
}
